package test;

import java.io.File;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.junit.Test;

import query.Query;
import query.QueryBuilder;

import document.DocumentLibrary;
import document.InvertedIndex;
import document.Stemmer;
import document.StopWordLexicon;


public class MissingSpaces {
	
	private static final String DOC_PATH = "TIME" + File.separator + "Docs";

	@Test
	public void findMisspelledWords() {
		DocumentLibrary library = new DocumentLibrary();
		library.initializeFrom(DOC_PATH);
		InvertedIndex index = new InvertedIndex();
		index.indexLibrary(library);
		
		Vector<String> errors = new Vector<String>();
		Iterator<String> terms = index.getAllIndexedTerms();
		Vector<String> stopwords = StopWordLexicon.getInstance().getAllStopWords();
				
		QueryBuilder qb = new QueryBuilder();
		Stemmer stemmer = new Stemmer();
		
		while (terms.hasNext()) {
			String term = terms.next().toLowerCase();
			for (String stopword : stopwords) {
				if(StopWordLexicon.getInstance().isStopWord(stemmer.stem(term))) {
					
				}
				else if(stopword.length()>5 && term.indexOf(stopword)==0) {
					errors.add("["+stopword+"] "+term);
				}
				else if(stopword.length()>5 && term.contains(stopword) && term.lastIndexOf(stopword) == term.length()-stopword.length()) {
					errors.add(term +" ["+stopword+"]");
				}

			}
			
		}
		
		System.out.println("possible errors are: ");
		int count = 1;
		for (String error : errors) {
			System.out.println(count++ + "\t" + error);
		}
		
	}
}
